﻿using System;
using System.Linq;
using System.Windows.Input;
using EasySL.Controls;
using EasySL.Entity;

namespace EasySL.UI.Controls
{
    public partial class ProductList
    {
        void OnCreate(object sender, MouseButtonEventArgs e)
        {
            ProductEdit editPanel = new ProductEdit();
            editPanel.Categorys = this.categorys;

            Window.ShowOpPanel(
                "Add Product",
                editPanel,
                panel =>
                {
                    this.products.Insert(0, editPanel.Product);

                    //this.addProductTask.Start();
                });
        }

        void OnUpdate(object sender, MouseButtonEventArgs e)
        {
            ProductEdit editPanel = new ProductEdit();
            editPanel.Categorys = this.categorys;
            editPanel.Product = this.productList.SelectedItem as Product;

            Window.ShowOpPanel(
                "Add Product",
                editPanel,
                panel =>
                {
                    //this.updateProductTask.Start();
                });
        }

        void OnDelete(object sender, MouseButtonEventArgs e)
        {
            var deleteProducts = products.Where(p => p.Checked).ToList();
            if (deleteProducts.Count == 0)
            {
                Window.Alert("Please checked the rows u want to delete");
            }
            else
            {
                Window.Confirm(
                    "Do u confirm to delete checked products?",
                    () =>
                    {
                        foreach (Product p in deleteProducts)
                            this.products.Remove(p);

                        //this.deleteProductTask.Start();
                    }
                );
            }
        }

        void OnRefresh(object sender, MouseButtonEventArgs e)
        {
            this.getProductListTask.Start();
        }
    }
}
